<?php

require(TEMPLATEPATH . '/widgets.php');
require(TEMPLATEPATH . '/shortcodes.php');

/**
 * Change who is able to manage_links
 */
function oss_filter_user_has_cap($caps, $cap, $context = null){
	global $current_user;
	static $allowedManageLinks = array(
		'jag3773' => true,
		'markgvh' => true
	);
	if(@$allowedManageLinks[$current_user->user_login]){
		$caps['manage_links'] = true;
	}
	return $caps;
}
add_filter('user_has_cap', 'oss_filter_user_has_cap', 10, 3);




/**
 * Template init
 */
function oss_init(){
	#Disable XHTML mode
	remove_filter('option_html_type', 'xhtml5support_filter_html_type');
	add_filter('gettext_with_context', 'ent2ncr');
}
add_action('init', 'oss_init');


/**
 * Browsers don't support named entities in XHTML5 yet, so replace a couple named entities with their values
 */
function oss_filter_replace_comment_entities($comment){
	#$comment = str_replace('&nbsp;', ' ', $comment);
	#$comment = str_replace('&raquo;', '»', $comment);
	#$comment = str_replace('&laquo;', '«', $comment);
	$comment = ent2ncr($comment);
	$comment = preg_replace('/&(?!(?:lt|gt|amp);)/i', '&amp;', $comment); #is this needed any more?
	return $comment;
}
add_filter('get_comment_text', "oss_filter_replace_comment_entities");
add_filter('get_comment_author_link', "oss_filter_replace_comment_entities");

/**
 * Add rel=next to next_posts_link
 */
function oss_add_rel_next_posts_link_attribute($attr){
	if(strpos($attr, 'rel=') !== false){
		return preg_replace('/(?<=rel=.)/', 'next ', $attr);
	}
	return "rel='next'";
}
add_filter('next_posts_link_attributes', 'oss_add_rel_next_posts_link_attribute');


/**
 * Add rel=next to next_post_link
 */
function oss_add_rel_next_post_link_attribute($el){
	if(strpos($el, 'rel=') !== false){
		return preg_replace('/(?<=rel=.)/', 'next ', $el);
	}
	return preg_replace('/(?<=<a\s)/', 'rel="next" ', $el);
}
add_filter('next_post_link', 'oss_add_rel_next_post_link_attribute');


/**
 * Add rel=prev to next_posts_link
 */
function oss_add_rel_prev_posts_link_attribute($attr){
	if(strpos($attr, 'rel=') !== false){
		return preg_replace('/(?<=rel=.)/', 'prev ', $attr);
	}
	return "rel='prev'";
}
add_filter('previous_posts_link_attributes', 'oss_add_rel_prev_posts_link_attribute');

/**
 * Add rel=next to previous_post_link
 */
function oss_add_rel_previous_post_link_attribute($el){
	if(strpos($el, 'rel=') !== false){
		return preg_replace('/(?<=rel=.)/', 'prev ', $el);
	}
	return preg_replace('/(?<=<a\s)/', 'rel="prev" ', $el);
}
add_filter('previous_post_link', 'oss_add_rel_previous_post_link_attribute');

/**
 * Add the commenter's gravatar
 */
function oss_filter_add_avatar_comment_author_link($link){
	$email = get_comment_author_email();
	if(!$email)
		return $link;
	$avatar = get_avatar($email, 80);
	if(strpos($link, '<a') !== false){
		return preg_replace("{(<a\b.+?)(>)}", "$1 target='_blank' title=\"Visit commenter's website\"$2" . $avatar, $link);
	}
	else {
		return $avatar . $link;
	}
	
}
add_filter('get_comment_author_link', "oss_filter_add_avatar_comment_author_link");